package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Rotation;
import com.example.demo.mapper.RotationMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/rotation")
public class RotationController {
    @Resource
    RotationMapper rotationMapper;


    @PostMapping
    public Result<?> save(@RequestBody Rotation rotation) {

        rotationMapper.insert(rotation);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody Rotation rotation) {
        rotationMapper.updateById(rotation);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        rotationMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Rotation> wrapper = Wrappers.<Rotation>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Rotation::getId, search);
        }
        Page<Rotation> stuPage = rotationMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }
    @GetMapping("/wx")
    public Result<?> rotation() {

         List<Rotation> rotationList = rotationMapper.selectList(null);
         return Result.success(rotationList);

    }

}
